package com.springmvc.web.pub.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springmvc.utils.JwtHelper;
import com.springmvc.utils.JwtInfo;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangRuChao
 * @since 2017-11-24
 */
@Controller
@RequestMapping("/")
public class IndexController {

	@Autowired
	JwtInfo jwtInfo;

	@ResponseBody
	@RequestMapping
	public String index(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return "Hello World!";
	}

	@ResponseBody
	@RequestMapping(value = "token")
	public String checkUserAndPassword(HttpServletRequest request) throws Exception {
		//登录成功
		String name = jwtInfo.getName();
		String userId = "123123";
		String role = "role";
		String audience = "audience";
		String issuer = "issuer";
		int TTLMillis = jwtInfo.getExpiresSecond();
		String base64Security = jwtInfo.getBase64Secret();
		String token = JwtHelper.createJWT(name, userId, role, audience, issuer, TTLMillis, base64Security);
		System.out.println(token);
		return token;
	}
}
